#include "string_operation.h"

/*
时间：2018 5 1
作者:latefly
功能：如下


这里要实现的函数有
StringReverse 字符串反转顺序
SplitString
IntString
StringInt
FloatString
StringFloat
DoubleString
StringDouble
StringEqual
StringCopy
HasSymbol 判断是否含有某一个字符串
StringTrim
StringLength

edit version 2018 7 23

*/




char STRING_OPERATION_BUFF[STRING_OPERATION_BUFF_DEEP][STRING_OPERATION_BUFF_SIZE] = {'\0'};


//下面是一些字符串的操作相关的函数
void string_buff_empty() {
    int i;
    for (i = 0; i < STRING_OPERATION_BUFF_DEEP; i++) {
        STRING_OPERATION_BUFF[i][0] = '\0';
    }
}

char get_string_buff_used_length() {
    char used = 0, i = 0;
    while (i < STRING_OPERATION_BUFF_DEEP) {
        if (STRING_OPERATION_BUFF[i][0] != '\0') {
            used++;
        }
        i++;
    }
    return used;
}

int string_length(char *s) {
    int Length = 0;
    while (*s != '\0') {
        s++;
        Length++;
    }
    return Length;
}

char string_trim(char symbol, char *s) {
    char isTrim = 0;
    char *p;
    p = s;
    while (*s != '\0') {
        if (*p != symbol) {
            isTrim = 1;
            *s = *p;
            s++;
            p++;
        } else
            p++;
    }
    *s = '\0';
    return isTrim;

}

char string_copy(char *source, char *target) {
    while (*source != '\0') {
        *target = *source;
        source++;
        target++;
    }
    *target = '\0';
    return 0;
}

char string_combine(char *source, char *target) {
    while (*target != '\0')target++;
    while (*source != '\0') {
        *target = *source;
        source++;
        target++;
    }
    *target = '\0';
    return 0;
}

//字符串顺序反转
char string_reverse(char *s) {
    char *p;
    p = STRING_OPERATION_BUFF[1];
    string_copy(s, STRING_OPERATION_BUFF[1]);
    while (*p != '\0')p++;
    p--;//跳过结束符
    while (*s != '\0') {
        *s = *p;
        s++;
        p--;
    }
    return 0;
}

//拆分以后 不含拆分符号
char string_split(char split, char *s) {
    char i = 0, j = 0;
    STRING_OPERATION_BUFF[0][0] = '\0';
    STRING_OPERATION_BUFF[1][0] = '\0';
    while (*s != '\0') {
        if (*s == split) {
            //j++;
            STRING_OPERATION_BUFF[i][j] = '\0';
            i++;
            j = 0;
            s++;
            continue;
        }
        STRING_OPERATION_BUFF[i][j] = *s;
        j++;
        s++;
    }
    STRING_OPERATION_BUFF[i][j] = '\0';
    return i;
}

char string_equal(char *s1, char *s2) {
    char isSame = 1;
    while (*s1 != '\0' && *s2 != '\0') {
        if (*s1 - *s2) {
            isSame = 0;
            break;
        }
        s1++;
        s2++;
    }
    if (*s1 != '\0' || *s2 != '\0') {
        isSame = 0;
    }
    return isSame;
}

char string_case_equal(char *s1, char *s2) {
    char isSame = 1;
    while (*s1 != '\0' && *s2 != '\0') {
        if ((*s1 > *s2) && (*s1 != (*s2 + 0x20))||(*s1 < *s2) && (*s2 != (*s1 + 0x20))) {
            isSame = 0;
            break;
        }
        s1++;
        s2++;
    }
    if (*s1 != '\0' || *s2 != '\0') {
        isSame = 0;
    }
    return isSame;
}

char has_symbol(char symbol, char *s) {
    char isHave = 0;
    while (*s != '\0') {
        if (*s == symbol) {
            isHave = 1;
            break;
        }
        s++;
    }
    return isHave;
}

char has_symbols_by_index(char *symbols, char *s, int index) {
    char isHave = 1;
    while (*s != '\0') {
        //跳转到指定位置
        if (index > 0) {
            index--;
            s++;
        } else {
            break;
        }
    }
    while (*symbols != '\0') {
        if (*s == '\0' || *symbols != *s) {
            isHave = -1;
            break;
        }
        symbols++;
        s++;
    }

    return isHave;
}

int string_find_symbol_index(char symbol, char *s) {
    int index = 0;
    while (*s != '\0') {
        //跳转到指定位置
        if (symbol == *s) {
            break;
        }
        index++;
        s++;
    }

    return index;
}

int string_to_int(char *s) {
    int num = 0;
    while (*s != '\0') {
        if (*s - '0' >= 0 && *s - '0' <= 9) {
            num *= 10;
            num += (int) (*s - '0');
        } else
            return 0;
        s++;
    }
    return num;
}

//转换结果有问题还未解决
char *int_to_string(int num) {
    char i = 0;
    while (num) {
        STRING_OPERATION_BUFF[0][i] = num % 10 + '0';
        i++;
        num /= 10;
    }
    STRING_OPERATION_BUFF[0][i] = '\0';
    string_reverse(STRING_OPERATION_BUFF[0]);
    return STRING_OPERATION_BUFF[0];//表示有几个数据被分离出来
}

float string_to_float(char *s) {
    float result = 0;
    float num = 0, pointer = 0;
    while (*s != '\0') {
        if (*s == '.') {
            break;
        }
        if (*s - '0' >= 0 && *s - '0' <= 9) {
            num *= 10;
            num += (float) (*s - '0');
        } else {
            result = 0;//TypeError
            return result;
        }
        s++;
    }
    if (has_symbol('.', s)) {
        while (*s != '\0')s++;//跳到尾
        s--;//跳过‘\0’
        while (*s != '.') {
            if (*s - '0' >= 0 && *s - '0' <= 9) {
                pointer /= 10;
                pointer += (double) ((*s - '0') / 10.0);
            } else {
                result = 0;
                return result;
            }
            s--;
        }
    }
    result = num + pointer;
    return result;
}

//此方法有问题
char *float_to_string(float number) {
    char j = 0;
    int num = (int) number;
    float pointer = number - num;
    while (num) {
        STRING_OPERATION_BUFF[0][j] = num % 10 + '0';
        j++;
        num /= 10;
    }
    STRING_OPERATION_BUFF[0][j] = '\0';
    string_reverse(STRING_OPERATION_BUFF[0]);
    if (pointer)
        STRING_OPERATION_BUFF[0][j] = '.';
    while (pointer) {
        pointer *= 10;
        num = (int) pointer;
        pointer -= num;
        j++;
        if (j > 40) {
            break;
        }
        STRING_OPERATION_BUFF[0][j] = num + '0';
    }
    j++;
    STRING_OPERATION_BUFF[0][j] = '\0';
    return STRING_OPERATION_BUFF[0];//返回注册
}

double string_to_double(char *s) {
    double result = 0;
    double num = 0, pointer = 0;
    while (*s != '\0') {
        if (*s == '.') {
            break;
        }
        if (*s - '0' >= 0 && *s - '0' <= 9) {
            num *= 10;
            num += (double) (*s - '0');
        } else {
            result = 0;//TypeError
            return result;
        }
        s++;
    }
    if (has_symbol('.', s)) {
        while (*s != '\0')s++;//跳到尾
        s--;//跳过‘\0’
        while (*s != '.') {
            if (*s - '0' >= 0 && *s - '0' <= 9) {
                pointer /= 10;
                pointer += (double) ((*s - '0') / 10.0);
            } else {
                result = 0;
                return result;
            }
            s--;
        }
    }
    result = num + pointer;
    return result;
}

char *double_to_string(double number) {
    char j = 0;
    int num = (int) number;
    double pointer = number - num;
    while (num) {
        STRING_OPERATION_BUFF[0][j] = num % 10 + '0';
        j++;
        num /= 10;
    }
    STRING_OPERATION_BUFF[0][j] = '\0';
    string_reverse(STRING_OPERATION_BUFF[0]);
    if (pointer)
        STRING_OPERATION_BUFF[0][j] = '.';
    while (pointer) {
        pointer *= 10;
        num = (int) pointer;
        pointer -= num;
        if (j > 40) {
            break;//防止越界
        }
        j++;
        STRING_OPERATION_BUFF[0][j] = num + '0';
    }
    j++;
    STRING_OPERATION_BUFF[0][j] = '\0';
    return STRING_OPERATION_BUFF[0];
}

char *string_replace(char *source, char *target, int index) {
    char *p = source;
    for (; index > 0; index--) {
        source++;
    }
    while (*target != '\0') {
        *source = *target;
        source++;
        target++;
    }
    *source = '\0';
    return p;
}


